<template>
  <view class="wxAttachment">
    <view class="file-name">
      <view v-for="(text, index) in texts" :key="index" :class="[`text${index}`]">
        <TImage
          v-if="index === 0"
          src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/attachmentIcon.png"
          width="80rpx"
          height="80rpx"
        ></TImage>
        <text v-else>{{ text }}</text>
      </view>
    </view>
    <view class="download" @click="handleDownload">查看文件</view>
  </view>
</template>
<script lang="ts">
  import { useUpload } from '@/hooks/core/useUpload';
  import TImage from '@/components/TImage/index.vue';

  export default {
    name: 'wxAttachment',
    components: { TImage },
    props: {
      node: {
        type: Object,
        default() {
          return {};
        },
      },
    },
    inject: ['parseSelect'],
    data() {
      return {};
    },
    computed: {
      texts() {
        return [, this.node.nodes[0].text.split('📁')[1]];
      },
      name() {
        return this.texts[1];
      },
      href() {
        return this.node.attr.href;
      },
      fileType() {
        return this.href.split('.').pop();
      },
    },

    methods: {
      handleDownload() {
        if (this.href && this.href !== 'undefined') {
          useUpload().openDocument({
            filePath: this.href,
            fileType: this.fileType,
            customFileName: this.name,
          });
        } else {
          uni.showModal({
            title: '提示',
            content: '文件地址不存在',
            showCancel: false,
          });
        }
      },
    },
  };
</script>
